﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SGD;
using System.Diagnostics;
using System.IO;
using System.Xml.Serialization;

namespace Animation_Editor
{
    public enum types { collision, frame, attack, anchor, nothing };

    public struct Frames
    {
        public string FrameName;
        public int FrameNumber;
        public int AnchorX;
        public int AnchorY;
        public int CollisionRectX;
        public int CollisionRectY;
        public int CollisionRectHeight;
        public int CollisionRectWidth;
        public int FrameRectX;
        public int FrameRectY;
        public int FrameRectHeight;
        public int FrameRectWidth;
        public int AttackRectX;
        public int AttackRectY;
        public int AttackRectHeight;
        public int AttackRectWidth;
        public Rectangle Collision;
        public Rectangle Frame;
        public Rectangle Attack;
        public bool AnchorPoint;
        public bool CollisionRect;
        public bool AttackRect;
        public bool FrameRect;
        public float time;
        public string Bitmap;
        public string AniName;
        public string TriName;
    }

    public partial class AnimationEditor : Form
    {
        public List<Frames> ListOfFrames = new List<Frames>();
        public int i;
        public int NumFrames = 0;
        public int changeFrame;
        public float Elapsedtime = 0;
        public float lasttime = 0;
        public float currentTime = 0;
        public Bitmap SpriteSheet;
        public int DRXSpriteSheet;
        public bool Looping = true;
        public bool IsPlaying = false;
        public bool DONE = false;
        public bool PathSet;
        public int WhatToDraw;
        public string FilePath;
        public string RelativePath;
        public int AnimationNameLength = 0;
        public int TriggerLength = 0;
        public int PathLength = 0;
        public Stopwatch timer = new Stopwatch();
        private ManagedDirect3D direct3DSpriteWindow = ManagedDirect3D.Instance;
        private ManagedTextureManager texturesprite = ManagedTextureManager.Instance;
        public Point Offset;

        public AnimationEditor()
        {
            InitializeComponent();

            FrameDuration.Enabled = false;
            AniRectX.Enabled = false;
            AniRectY.Enabled = false;
            AniWidth.Enabled = false;
            AniHeight.Enabled = false;
            AniPointX.Enabled = false;
            AniPointY.Enabled = false;
            AniFrameRect.Enabled = false;
            AniCollisionRect.Enabled = false;
            AniAttackRect.Enabled = false;
            AniAnchorPoint.Enabled = false;
            AniImage.Enabled = false;
            AniAdd.Enabled = false;
            AniRemove.Enabled = false;
            AniNexFrame.Enabled = false;
            AniPrevFrame.Enabled = false;
            AniReset.Enabled = false;
            AniPlay.Enabled = false;
            AniStop.Enabled = false;
            TriggerName.Enabled = false;
            PathSet = false;

            RelativePath = Environment.CurrentDirectory;
            i = 1;
            Offset.X = 200;
            Offset.Y = 150;
            changeFrame = 0;
            SpriteSheet = null;
            direct3DSpriteWindow.InitManagedDirect3D(AniPlayback, false);
            direct3DSpriteWindow.InitManagedDirect3D(AniImage, false);
            texturesprite.InitManagedTextureManager(direct3DSpriteWindow.Device, direct3DSpriteWindow.Sprite);
            timer.Start();
        }
        public void Update()
        {
            Elapsedtime = timer.ElapsedMilliseconds - lasttime;
            lasttime = timer.ElapsedMilliseconds;
            if (IsPlaying)
            {
                if (ListOfFrames[changeFrame].FrameRect && ListOfFrames[changeFrame].AnchorPoint)
                {
                    currentTime -= Elapsedtime / 1000;
                    if (currentTime <= 0)
                    {
                        changeFrame += 1;
                        if (changeFrame == ListOfFrames.Count)
                        {
                            changeFrame = 0;
                        }
                        currentTime = ListOfFrames[changeFrame].time;
                    }
                }
                else
                    changeFrame += 1;
            }
        }
        public void Render()
        {
            direct3DSpriteWindow.Clear(AniPlayback, 255, 255, 255);
            direct3DSpriteWindow.DeviceBegin();
            direct3DSpriteWindow.SpriteBegin();

            if (IsPlaying)
            {
                if (changeFrame == ListOfFrames.Count)
                {
                    changeFrame = 0;
                }
                else
                {
                    if (ListOfFrames[changeFrame].FrameRect && ListOfFrames[changeFrame].AnchorPoint)
                    {
                        texturesprite.Draw(DRXSpriteSheet, (int)(ListOfFrames[changeFrame].FrameRectX - ListOfFrames[changeFrame].AnchorX) + Offset.X,
                            (int)(ListOfFrames[changeFrame].FrameRectY - ListOfFrames[changeFrame].AnchorY) + Offset.Y, 1, 1, ListOfFrames[changeFrame].Frame,
                            ListOfFrames[changeFrame].Frame.Width / 2, ListOfFrames[changeFrame].Frame.Height / 2, 0, 0);
                    }
                }

            }
            else
            {
                if (AniFrames.Items.Count != 0)
                {
                    if (ListOfFrames[changeFrame].FrameRect && ListOfFrames[changeFrame].AnchorPoint)
                    {
                        texturesprite.Draw(DRXSpriteSheet, (int)(ListOfFrames[changeFrame].FrameRectX - ListOfFrames[changeFrame].AnchorX) + Offset.X,
                            (int)(ListOfFrames[changeFrame].FrameRectY - ListOfFrames[changeFrame].AnchorY) + Offset.Y, 1, 1, ListOfFrames[changeFrame].Frame,
                            ListOfFrames[changeFrame].Frame.Width / 2, ListOfFrames[changeFrame].Frame.Height / 2, 0, 0);
                    }
                }
            }
            direct3DSpriteWindow.SpriteEnd();
            direct3DSpriteWindow.DeviceEnd();
            direct3DSpriteWindow.Present(AniPlayback);

            //direct3DSpriteWindow.Clear(AniImage, 255, 255, 255);
            //direct3DSpriteWindow.DeviceBegin();
            //direct3DSpriteWindow.SpriteBegin();

            //if (SpriteSheet != null)
            //{
            //    Rectangle window = new Rectangle(hScrollBar.Value, vScrollBar.Value, AniImage.Width, AniImage.Height);
            //    texturesprite.Draw(DRXSpriteSheet, 0, 0, 1, 1, window, 0, 0, 0, 0);
            //}

            //direct3DSpriteWindow.SpriteEnd();

            //foreach (Frames frame in ListOfFrames)
            //{
            //    direct3DSpriteWindow.LineBegin();
            //    if (frame.FrameRect)
            //    {
            //        direct3DSpriteWindow.DrawLine(frame.Frame.X, frame.Frame.Y, frame.FrameRectWidth, frame.FrameRectWidth, 0, 0, 0);
            //    }
            //    direct3DSpriteWindow.LineEnd();
            //}
            //direct3DSpriteWindow.DeviceEnd();
            //direct3DSpriteWindow.Present(AniImage);
        }
        private void AniPlayback_MouseDown(object sender, MouseEventArgs e)
        {
            Offset.X = e.Location.X;
            Offset.Y = e.Location.Y;
        }
        private void AniPlay_Click(object sender, EventArgs e)
        {
            if (ListOfFrames.Count != 0)
            {
                IsPlaying = true;
                currentTime = ListOfFrames[changeFrame].time;
                FrameDuration.Enabled = false;
                AniRectX.Enabled = false;
                AniRectY.Enabled = false;
                AniWidth.Enabled = false;
                AniHeight.Enabled = false;
                AniPointX.Enabled = false;
                AniPointY.Enabled = false;
                AniFrameRect.Enabled = false;
                AniCollisionRect.Enabled = false;
                AniAttackRect.Enabled = false;
                AniAnchorPoint.Enabled = false;
                AniImage.Enabled = false;
                AniAdd.Enabled = false;
                AniRemove.Enabled = false;
                AniNexFrame.Enabled = false;
                AniPrevFrame.Enabled = false;
            }
        }

        private void AniStop_Click(object sender, EventArgs e)
        {
            IsPlaying = false;
            FrameDuration.Visible = true;
            FrameDuration.Enabled = true;
            AniRectX.Enabled = true;
            AniRectY.Enabled = true;
            AniWidth.Enabled = true;
            AniHeight.Enabled = true;
            AniPointX.Enabled = true;
            AniPointY.Enabled = true;
            AniFrameRect.Enabled = true;
            AniCollisionRect.Enabled = true;
            AniAttackRect.Enabled = true;
            AniAnchorPoint.Enabled = true;
            AniImage.Enabled = true;
            AniAdd.Enabled = true;
            AniRemove.Enabled = true;
            AniNexFrame.Enabled = true;
            AniPrevFrame.Enabled = true;
        }

        private void AniReset_Click(object sender, EventArgs e)
        {
            changeFrame = 0;
        }

        private void AniNexFrame_Click(object sender, EventArgs e)
        {
            if (changeFrame == (ListOfFrames.Count-1))
            {
                changeFrame = 0;
            }
            else
            {
                changeFrame += 1;
            }
        }

        private void AniPrevFrame_Click(object sender, EventArgs e)
        {
            if (changeFrame == 0)
            {
                changeFrame = (ListOfFrames.Count - 1);
            }
            else
            {
                changeFrame -= 1;
            }
        }

        private void FrameDuration_ValueChanged(object sender, EventArgs e)
        {
            for (int j = 0; j < ListOfFrames.Count; j++)
            {
                if (AniFrames.GetSelected(j))
                {
                    Frames frame = ListOfFrames[j];
                    frame.time = (float)FrameDuration.Value;
                    ListOfFrames.Remove(ListOfFrames[j]);
                    ListOfFrames.Insert(j, frame);
                }
            }
        }
        private void AniAdd_Click(object sender, EventArgs e)
        {
            if (SpriteSheet != null)
            {
                if (IsPlaying == false)
                {
                    Frames tempFrame = new Frames();

                    tempFrame.FrameName = "frame ";
                    tempFrame.FrameNumber = i;
                    tempFrame.AnchorX = 0;
                    tempFrame.AnchorY = 0;
                    tempFrame.CollisionRectX = 0;
                    tempFrame.CollisionRectY = 0;
                    tempFrame.CollisionRectHeight = 0;
                    tempFrame.CollisionRectWidth = 0;
                    tempFrame.AttackRectX = 0;
                    tempFrame.AttackRectY = 0;
                    tempFrame.AttackRectHeight = 0;
                    tempFrame.AttackRectWidth = 0;
                    tempFrame.FrameRectX = 0;
                    tempFrame.FrameRectY = 0;
                    tempFrame.FrameRectHeight = 0;
                    tempFrame.FrameRectWidth = 0;
                    tempFrame.CollisionRect = false;
                    tempFrame.FrameRect = false;
                    tempFrame.AttackRect = false;
                    tempFrame.AnchorPoint = false;
                    tempFrame.time = (float)FrameDuration.Value;
                    WhatToDraw = (int)types.nothing;
                    AniFrames.Items.Add(tempFrame.FrameName + tempFrame.FrameNumber);
                    ListOfFrames.Add(tempFrame);
                    AniFrames.SelectedIndex = (ListOfFrames.Count - 1);
                    i += 1;
                    NumFrames += 1;
                }
            }
        }

        private void AniRemove_Click(object sender, EventArgs e)
        {
            if (SpriteSheet != null)
            {
                if (IsPlaying == false)
                {
                    for (int j = 0; j < AniFrames.Items.Count; j++)
                    {
                        if (AniFrames.GetSelected(j))
                        {
                            if (AniFrames.SelectedItem.Equals(ListOfFrames[j].FrameName + ListOfFrames[j].FrameNumber))
                            {
                                if (ListOfFrames[j].FrameNumber == AniFrames.Items.Count)
                                {
                                    AniFrames.Items.Remove(AniFrames.SelectedItem);
                                    ListOfFrames.Remove(ListOfFrames[j]);
                                    AniImage.Invalidate();
                                    --i;
                                    AniFrames.SelectedIndex = j - 1;
                                    --NumFrames;
                                }
                                else
                                {
                                    AniFrames.Items.Remove(AniFrames.SelectedItem);
                                    ListOfFrames.RemoveAt(j);
                                    AniImage.Invalidate();
                                    AniFrames.SelectedIndex = j - 1;
                                    --NumFrames;
                                }
                            }
                        }
                    }
                }
            }
        }

        private void AniRectX_ValueChanged(object sender, EventArgs e)
        {
            if (IsPlaying == false)
            {
                switch (WhatToDraw)
                {
                    case (int)types.collision:
                        {
                            for (int j = 0; j < AniFrames.Items.Count; j++)
                            {
                                if (AniFrames.GetSelected(j))
                                {
                                    if (AniFrames.SelectedItem.Equals(ListOfFrames[j].FrameName + ListOfFrames[j].FrameNumber))
                                    {
                                        Frames frame = ListOfFrames[j];
                                        frame.CollisionRectX = (int)AniRectX.Value;
                                        frame.Collision.X = (int)frame.CollisionRectX;
                                        ListOfFrames.Remove(ListOfFrames[j]);
                                        ListOfFrames.Insert(j, frame);
                                        AniImage.Invalidate();
                                    }
                                }
                            }
                        }
                        break;
                    case (int)types.frame:
                        {
                            for (int j = 0; j < AniFrames.Items.Count; j++)
                            {
                                if (AniFrames.GetSelected(j))
                                {
                                    if (AniFrames.SelectedItem.Equals(ListOfFrames[j].FrameName + ListOfFrames[j].FrameNumber))
                                    {
                                        Frames frame = ListOfFrames[j];
                                        frame.FrameRectX = (int)AniRectX.Value;
                                        frame.Frame.X = (int)frame.FrameRectX;
                                        ListOfFrames.Remove(ListOfFrames[j]);
                                        ListOfFrames.Insert(j, frame);
                                        AniImage.Invalidate();
                                    }
                                }
                            }
                        }
                        break;
                    case (int)types.attack:
                        {
                            for (int j = 0; j < AniFrames.Items.Count; j++)
                            {
                                if (AniFrames.GetSelected(j))
                                {
                                    if (AniFrames.SelectedItem.Equals(ListOfFrames[j].FrameName + ListOfFrames[j].FrameNumber))
                                    {
                                        Frames frame = ListOfFrames[j];
                                        frame.AttackRectX = (int)AniRectX.Value;
                                        frame.Attack.X = (int)frame.AttackRectX;
                                        ListOfFrames.Remove(ListOfFrames[j]);
                                        ListOfFrames.Insert(j, frame);
                                        AniImage.Invalidate();
                                    }
                                }
                            }
                        }
                        break;
                }
            }
        }

        private void AniRectY_ValueChanged(object sender, EventArgs e)
        {
            if (IsPlaying == false)
            {
                switch (WhatToDraw)
                {
                    case (int)types.collision:
                        {
                            for (int j = 0; j < AniFrames.Items.Count; j++)
                            {
                                if (AniFrames.GetSelected(j))
                                {
                                    if (AniFrames.SelectedItem.Equals(ListOfFrames[j].FrameName + ListOfFrames[j].FrameNumber))
                                    {
                                        Frames frame = ListOfFrames[j];
                                        frame.CollisionRectY = (int)AniRectY.Value;
                                        frame.Collision.Y = (int)frame.CollisionRectY;
                                        ListOfFrames.Remove(ListOfFrames[j]);
                                        ListOfFrames.Insert(j, frame);
                                        AniImage.Invalidate();
                                    }
                                }
                            }
                        }
                        break;
                    case (int)types.frame:
                        {
                            for (int j = 0; j < AniFrames.Items.Count; j++)
                            {
                                if (AniFrames.GetSelected(j))
                                {
                                    if (AniFrames.SelectedItem.Equals(ListOfFrames[j].FrameName + ListOfFrames[j].FrameNumber))
                                    {
                                        Frames frame = ListOfFrames[j];
                                        frame.FrameRectY = (int)AniRectY.Value;
                                        frame.Frame.Y = (int)frame.FrameRectY;
                                        ListOfFrames.Remove(ListOfFrames[j]);
                                        ListOfFrames.Insert(j, frame);
                                        AniImage.Invalidate();
                                    }
                                }
                            }
                        }
                        break;
                    case (int)types.attack:
                        {
                            for (int j = 0; j < AniFrames.Items.Count; j++)
                            {
                                if (AniFrames.GetSelected(j))
                                {
                                    if (AniFrames.SelectedItem.Equals(ListOfFrames[j].FrameName + ListOfFrames[j].FrameNumber))
                                    {
                                        Frames frame = ListOfFrames[j];
                                        frame.AttackRectY = (int)AniRectY.Value;
                                        frame.Attack.Y = (int)frame.AttackRectY;
                                        ListOfFrames.Remove(ListOfFrames[j]);
                                        ListOfFrames.Insert(j, frame);
                                        AniImage.Invalidate();
                                    }
                                }
                            }
                        }
                        break;
                }
            }
        }

        private void AniHeight_ValueChanged(object sender, EventArgs e)
        {
            if (IsPlaying == false)
            {
                switch (WhatToDraw)
                {
                    case (int)types.collision:
                        {
                            for (int j = 0; j < AniFrames.Items.Count; j++)
                            {
                                if (AniFrames.GetSelected(j))
                                {
                                    if (AniFrames.SelectedItem.Equals(ListOfFrames[j].FrameName + ListOfFrames[j].FrameNumber))
                                    {
                                        Frames frame = ListOfFrames[j];
                                        frame.CollisionRectHeight = (int)AniHeight.Value;
                                        frame.Collision.Height = (int)frame.CollisionRectHeight;
                                        ListOfFrames.Remove(ListOfFrames[j]);
                                        ListOfFrames.Insert(j, frame);
                                        AniImage.Invalidate();
                                    }
                                }
                            }
                        }
                        break;
                    case (int)types.frame:
                        {
                            for (int j = 0; j < AniFrames.Items.Count; j++)
                            {
                                if (AniFrames.GetSelected(j))
                                {
                                    if (AniFrames.SelectedItem.Equals(ListOfFrames[j].FrameName + ListOfFrames[j].FrameNumber))
                                    {
                                        Frames frame = ListOfFrames[j];
                                        frame.FrameRectHeight = (int)AniHeight.Value;
                                        frame.Frame.Height = (int)frame.FrameRectHeight;
                                        ListOfFrames.Remove(ListOfFrames[j]);
                                        ListOfFrames.Insert(j, frame);
                                        AniImage.Invalidate();
                                    }
                                }
                            }
                        }
                        break;
                    case (int)types.attack:
                        {
                            for (int j = 0; j < AniFrames.Items.Count; j++)
                            {
                                if (AniFrames.GetSelected(j))
                                {
                                    if (AniFrames.SelectedItem.Equals(ListOfFrames[j].FrameName + ListOfFrames[j].FrameNumber))
                                    {
                                        Frames frame = ListOfFrames[j];
                                        frame.AttackRectHeight = (int)AniHeight.Value;
                                        frame.Attack.Height = (int)frame.AttackRectHeight;
                                        ListOfFrames.Remove(ListOfFrames[j]);
                                        ListOfFrames.Insert(j, frame);
                                        AniImage.Invalidate();
                                    }
                                }
                            }
                        }
                        break;
                }
            }
        }

        private void AniWidth_ValueChanged(object sender, EventArgs e)
        {
            if (IsPlaying == false)
            {
                switch (WhatToDraw)
                {
                    case (int)types.collision:
                        {
                            for (int j = 0; j < AniFrames.Items.Count; j++)
                            {
                                if (AniFrames.GetSelected(j))
                                {
                                    if (AniFrames.SelectedItem.Equals(ListOfFrames[j].FrameName + ListOfFrames[j].FrameNumber))
                                    {
                                        Frames frame = ListOfFrames[j];
                                        frame.CollisionRectWidth = (int)AniWidth.Value;
                                        frame.Collision.Width = (int)frame.CollisionRectWidth;
                                        ListOfFrames.Remove(ListOfFrames[j]);
                                        ListOfFrames.Insert(j, frame);
                                        AniImage.Invalidate();
                                    }
                                }
                            }
                        }
                        break;
                    case (int)types.frame:
                        {
                            for (int j = 0; j < AniFrames.Items.Count; j++)
                            {
                                if (AniFrames.GetSelected(j))
                                {
                                    if (AniFrames.SelectedItem.Equals(ListOfFrames[j].FrameName + ListOfFrames[j].FrameNumber))
                                    {
                                        Frames frame = ListOfFrames[j];
                                        frame.FrameRectWidth = (int)AniWidth.Value;
                                        frame.Frame.Width = (int)frame.FrameRectWidth;
                                        ListOfFrames.Remove(ListOfFrames[j]);
                                        ListOfFrames.Insert(j, frame);
                                        AniImage.Invalidate();
                                    }
                                }
                            }
                        }
                        break;
                    case (int)types.attack:
                        {
                            for (int j = 0; j < AniFrames.Items.Count; j++)
                            {
                                if (AniFrames.GetSelected(j))
                                {
                                    if (AniFrames.SelectedItem.Equals(ListOfFrames[j].FrameName + ListOfFrames[j].FrameNumber))
                                    {
                                        Frames frame = ListOfFrames[j];
                                        frame.AttackRectWidth = (int)AniWidth.Value;
                                        frame.Attack.Width = (int)frame.AttackRectWidth;
                                        ListOfFrames.Remove(ListOfFrames[j]);
                                        ListOfFrames.Insert(j, frame);
                                        AniImage.Invalidate();
                                    }
                                }
                            }
                        }
                        break;
                }
            }
        }

        private void AniPointX_ValueChanged(object sender, EventArgs e)
        {
            if (IsPlaying == false)
            {
                for (int j = 0; j < AniFrames.Items.Count; j++)
                {
                    if (AniFrames.GetSelected(j))
                    {
                        if (AniFrames.SelectedItem.Equals(ListOfFrames[j].FrameName + ListOfFrames[j].FrameNumber))
                        {
                            Frames frame = ListOfFrames[j];
                            frame.AnchorX = (int)AniPointX.Value;
                            ListOfFrames.Remove(ListOfFrames[j]);
                            ListOfFrames.Insert(j, frame);
                            AniImage.Invalidate();
                        }
                    }
                }
            }
        }

        private void AniPointY_ValueChanged(object sender, EventArgs e)
        {
            if (IsPlaying == false)
            {
                for (int j = 0; j < AniFrames.Items.Count; j++)
                {
                    if (AniFrames.GetSelected(j))
                    {
                        if (AniFrames.SelectedItem.Equals(ListOfFrames[j].FrameName + ListOfFrames[j].FrameNumber))
                        {
                            Frames frame = ListOfFrames[j];
                            frame.AnchorY = (int)AniPointY.Value;
                            ListOfFrames.Remove(ListOfFrames[j]);
                            ListOfFrames.Insert(j, frame);
                            AniImage.Invalidate();
                        }
                    }
                }
            }
        }

        private void AniFrames_SelectedIndexChanged(object sender, EventArgs e)
        {
            for (int j = 0; j < AniFrames.Items.Count; j++)
            {
                if (AniFrames.GetSelected(j))
                {
                    if (AniFrames.SelectedItem.Equals(ListOfFrames[j].FrameName + ListOfFrames[j].FrameNumber))
                    {
                        FrameDuration.Value = (decimal)ListOfFrames[j].time;

                        switch (WhatToDraw)
                        {
                            case (int)types.collision:
                                {
                                    if (ListOfFrames[j].CollisionRect)
                                    {
                                        AniRectX.Value = ListOfFrames[j].Collision.X;
                                        AniRectY.Value = ListOfFrames[j].Collision.Y;
                                        AniHeight.Value = ListOfFrames[j].Collision.Height;
                                        AniWidth.Value = ListOfFrames[j].Collision.Width;
                                    }
                                }
                                break;
                            case (int)types.frame:
                                {
                                    if (ListOfFrames[j].FrameRect)
                                    {
                                        AniRectX.Value = ListOfFrames[j].Frame.X;
                                        AniRectY.Value = ListOfFrames[j].Frame.Y;
                                        AniHeight.Value = ListOfFrames[j].Frame.Height;
                                        AniWidth.Value = ListOfFrames[j].Frame.Width;
                                    }
                                }
                                break;
                            case (int)types.attack:
                                {
                                    if (ListOfFrames[j].AttackRect)
                                    {
                                        AniRectX.Value = ListOfFrames[j].Attack.X;
                                        AniRectY.Value = ListOfFrames[j].Attack.Y;
                                        AniHeight.Value = ListOfFrames[j].Attack.Height;
                                        AniWidth.Value = ListOfFrames[j].Attack.Width;
                                    }
                                }
                                break;
                            case (int)types.anchor:
                                {
                                    if (ListOfFrames[j].AnchorPoint)
                                    {
                                        AniPointX.Value = ListOfFrames[j].AnchorX;
                                        AniPointY.Value = ListOfFrames[j].AnchorY;
                                    }
                                }
                                break;
                        }
                    }
                    AniImage.Invalidate();
                }
            }
        }

        private void importSheetToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (PathSet)
            {
                OpenFileDialog dlg = new OpenFileDialog();
                dlg.InitialDirectory = RelativePath;
                if (DialogResult.OK == dlg.ShowDialog())
                {
                    SpriteSheet = new Bitmap(dlg.FileName);
                    DRXSpriteSheet = texturesprite.LoadTexture(dlg.FileName, 0);
                    vScrollBar.Maximum = SpriteSheet.Height;
                    vScrollBar.Minimum = 0;
                    hScrollBar.Maximum = SpriteSheet.Width;
                    hScrollBar.Minimum = 0;
                    FilePath = dlg.FileName;
                    AniImage.Invalidate();
                }
                FrameDuration.Enabled = true;
                AniRectX.Enabled = true;
                AniRectY.Enabled = true;
                AniWidth.Enabled = true;
                AniHeight.Enabled = true;
                AniPointX.Enabled = true;
                AniPointY.Enabled = true;
                AniFrameRect.Enabled = true;
                AniCollisionRect.Enabled = true;
                AniAttackRect.Enabled = true;
                AniAnchorPoint.Enabled = true;
                AniImage.Enabled = true;
                AniAdd.Enabled = true;
                AniRemove.Enabled = true;
                AniNexFrame.Enabled = true;
                AniPrevFrame.Enabled = true;
                AniReset.Enabled = true;
                AniPlay.Enabled = true;
                AniStop.Enabled = true;
                TriggerName.Enabled = true;
            }
            else
            {
                MessageBox.Show("Set a relative path through the import file option. Then import a bitmap or load in a file.", "Trust me, Im a doctor.", 
                    MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
        }

        private void AniImage_Paint(object sender, PaintEventArgs e)
        {
            Pen Collisionpen = new Pen(Color.Blue, 3);
            Pen Framepen = new Pen(Color.Black, 3);
            Pen Attackpen = new Pen(Color.Red, 3);
            Pen Anchorpen = new Pen(Color.Yellow);
            Rectangle window = new Rectangle(hScrollBar.Value, vScrollBar.Value, AniImage.Width, AniImage.Height);
            
            if (SpriteSheet != null)
            {
                e.Graphics.DrawImage(SpriteSheet,0,0,window,GraphicsUnit.Pixel);

                for (int j = 0; j < ListOfFrames.Count; j++)
                {
                    if (AniFrames.GetSelected(j))
                    {
                        if (ListOfFrames[j].CollisionRect)
                        {
                            e.Graphics.DrawRectangle(Collisionpen, ListOfFrames[j].Collision.X - hScrollBar.Value, ListOfFrames[j].Collision.Y - vScrollBar.Value,
                                ListOfFrames[j].Collision.Width, ListOfFrames[j].Collision.Height);
                        }
                        if (ListOfFrames[j].FrameRect)
                        {
                            e.Graphics.DrawRectangle(Framepen, ListOfFrames[j].Frame.X - hScrollBar.Value, ListOfFrames[j].Frame.Y - vScrollBar.Value,
                                ListOfFrames[j].Frame.Width, ListOfFrames[j].Frame.Height);
                        }
                        if (ListOfFrames[j].AttackRect)
                        {
                            e.Graphics.DrawRectangle(Attackpen, ListOfFrames[j].Attack.X - hScrollBar.Value, ListOfFrames[j].Attack.Y - vScrollBar.Value,
                                ListOfFrames[j].Attack.Width, ListOfFrames[j].Attack.Height);
                        }
                        if (ListOfFrames[j].AnchorPoint)
                        {
                            e.Graphics.FillEllipse(Anchorpen.Brush, (int)ListOfFrames[j].AnchorX - hScrollBar.Value,
                                (int)ListOfFrames[j].AnchorY - vScrollBar.Value, 10, 10);
                        }
                    }
                }
            }
        }

        private void AniImage_MouseDown(object sender, MouseEventArgs e)
        {
            if (SpriteSheet != null || AniFrames.Items.Count == 0)
            {
                if (IsPlaying == false)
                {
                    switch (WhatToDraw)
                    {
                        case (int)types.collision:
                            {
                                for (int j = 0; j < ListOfFrames.Count; j++)
                                {
                                    if (AniFrames.SelectedItem.Equals(ListOfFrames[j].FrameName + ListOfFrames[j].FrameNumber))
                                    {
                                        Frames frame = ListOfFrames[j];
                                        frame.CollisionRectX = e.Location.X + hScrollBar.Value;
                                        frame.CollisionRectY = e.Location.Y + vScrollBar.Value;
                                        AniRectX.Value = e.Location.X + hScrollBar.Value;
                                        AniRectY.Value = e.Location.Y + vScrollBar.Value;
                                        ListOfFrames.Remove(ListOfFrames[j]);
                                        ListOfFrames.Insert(j, frame);
                                    }
                                }
                            }
                            break;
                        case (int)types.frame:
                            {
                                for (int j = 0; j < ListOfFrames.Count; j++)
                                {
                                    if (AniFrames.SelectedItem.Equals(ListOfFrames[j].FrameName + ListOfFrames[j].FrameNumber))
                                    {
                                        Frames frame = ListOfFrames[j];
                                        frame.FrameRectX = e.Location.X + hScrollBar.Value;
                                        frame.FrameRectY = e.Location.Y + vScrollBar.Value;
                                        AniRectX.Value = e.Location.X + hScrollBar.Value;
                                        AniRectY.Value = e.Location.Y + vScrollBar.Value;
                                        ListOfFrames.Remove(ListOfFrames[j]);
                                        ListOfFrames.Insert(j, frame);
                                    }
                                }
                            }
                            break;
                        case (int)types.attack:
                            {
                                for (int j = 0; j < ListOfFrames.Count; j++)
                                {
                                    if (AniFrames.SelectedItem.Equals(ListOfFrames[j].FrameName + ListOfFrames[j].FrameNumber))
                                    {
                                        Frames frame = ListOfFrames[j];
                                        frame.AttackRectX = e.Location.X + hScrollBar.Value;
                                        frame.AttackRectY = e.Location.Y + vScrollBar.Value;
                                        AniRectX.Value = e.Location.X + hScrollBar.Value;
                                        AniRectY.Value = e.Location.Y + vScrollBar.Value;
                                        ListOfFrames.Remove(ListOfFrames[j]);
                                        ListOfFrames.Insert(j, frame);
                                    }
                                }
                            }
                            break;
                        case (int)types.anchor:
                            {
                                for (int j = 0; j < ListOfFrames.Count; j++)
                                {
                                    if (AniFrames.SelectedItem.Equals(ListOfFrames[j].FrameName + ListOfFrames[j].FrameNumber))
                                    {
                                        Frames frame = ListOfFrames[j];
                                        frame.AnchorX = e.Location.X + hScrollBar.Value;
                                        frame.AnchorY = e.Location.Y + vScrollBar.Value;
                                        AniPointX.Value = e.Location.X + hScrollBar.Value;
                                        AniPointY.Value = e.Location.Y + vScrollBar.Value;
                                        frame.AnchorPoint = true;
                                        ListOfFrames.Remove(ListOfFrames[j]);
                                        ListOfFrames.Insert(j, frame);
                                    }
                                }
                            }
                            break;
                    }
                }
            }
        }
        private void AniImage_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (SpriteSheet != null || AniFrames.Items.Count == 0)
                {
                    if (IsPlaying == false)
                    {
                        switch (WhatToDraw)
                        {
                            case (int)types.collision:
                                {
                                    for (int j = 0; j < ListOfFrames.Count; j++)
                                    {
                                        if (AniFrames.SelectedItem.Equals(ListOfFrames[j].FrameName + ListOfFrames[j].FrameNumber))
                                        {
                                            Frames frame = ListOfFrames[j];

                                            if (e.X + hScrollBar.Value > ListOfFrames[j].CollisionRectX && e.Y + vScrollBar.Value > ListOfFrames[j].CollisionRectY)
                                            {
                                                frame.Collision.X = (int)ListOfFrames[j].CollisionRectX;
                                                frame.Collision.Y = (int)ListOfFrames[j].CollisionRectY;
                                                frame.Collision.Height = (e.Location.Y + vScrollBar.Value) - (int)ListOfFrames[j].CollisionRectY;
                                                frame.Collision.Width = (e.Location.X + hScrollBar.Value) - (int)ListOfFrames[j].CollisionRectX;
                                                AniWidth.Value = frame.Collision.Width;
                                                AniHeight.Value = frame.Collision.Height;
                                            }
                                            if (e.X + hScrollBar.Value < ListOfFrames[j].CollisionRectX && e.Y + vScrollBar.Value > ListOfFrames[j].CollisionRectY)
                                            {
                                                frame.Collision.X = e.Location.X + hScrollBar.Value;
                                                frame.Collision.Y = (int)ListOfFrames[j].CollisionRectY;
                                                frame.Collision.Height = (e.Location.Y + vScrollBar.Value) - (int)ListOfFrames[j].CollisionRectY;
                                                frame.Collision.Width = ((int)ListOfFrames[j].CollisionRectX - frame.Collision.X);
                                                AniWidth.Value = frame.Collision.Width;
                                                AniHeight.Value = frame.Collision.Height;
                                            }
                                            if (e.X + hScrollBar.Value < ListOfFrames[j].CollisionRectX && e.Y + vScrollBar.Value < ListOfFrames[j].CollisionRectY)
                                            {
                                                frame.Collision.X = e.Location.X + hScrollBar.Value;
                                                frame.Collision.Y = e.Location.Y + vScrollBar.Value;
                                                frame.Collision.Height = ((int)ListOfFrames[j].CollisionRectY - frame.Collision.Y);
                                                frame.Collision.Width = ((int)ListOfFrames[j].CollisionRectX - frame.Collision.X);
                                                AniWidth.Value = frame.Collision.Width;
                                                AniHeight.Value = frame.Collision.Height;
                                            }
                                            if (e.X + hScrollBar.Value > ListOfFrames[j].CollisionRectX && e.Y + vScrollBar.Value < ListOfFrames[j].CollisionRectY)
                                            {
                                                frame.Collision.X = (int)ListOfFrames[j].CollisionRectX;
                                                frame.Collision.Y = e.Location.Y + vScrollBar.Value;
                                                frame.Collision.Height = ((int)ListOfFrames[j].CollisionRectY - frame.Collision.Y);
                                                frame.Collision.Width = (e.Location.X + hScrollBar.Value) - (int)ListOfFrames[j].CollisionRectX;
                                                AniWidth.Value = frame.Collision.Width;
                                                AniHeight.Value = frame.Collision.Height;
                                            }
                                            frame.CollisionRect = true;
                                            ListOfFrames.Remove(ListOfFrames[j]);
                                            ListOfFrames.Insert(j, frame);
                                            //AniImage.Invalidate();
                                        }
                                    }
                                }
                                break;
                            case (int)types.frame:
                                {
                                    for (int j = 0; j < ListOfFrames.Count; j++)
                                    {
                                        if (AniFrames.SelectedItem.Equals(ListOfFrames[j].FrameName + ListOfFrames[j].FrameNumber))
                                        {
                                            Frames frame = ListOfFrames[j];
                                            if (e.X + hScrollBar.Value > ListOfFrames[j].FrameRectX && e.Y + vScrollBar.Value > ListOfFrames[j].FrameRectY)
                                            {
                                                frame.Frame.X = (int)ListOfFrames[j].FrameRectX;
                                                frame.Frame.Y = (int)ListOfFrames[j].FrameRectY;
                                                frame.Frame.Height = (e.Location.Y + vScrollBar.Value) - (int)ListOfFrames[j].FrameRectY;
                                                frame.Frame.Width = (e.Location.X + hScrollBar.Value) - (int)ListOfFrames[j].FrameRectX;
                                                AniWidth.Value = frame.Frame.Width;
                                                AniHeight.Value = frame.Frame.Height;
                                            }
                                            if (e.X + hScrollBar.Value < ListOfFrames[j].FrameRectX && e.Y + vScrollBar.Value > ListOfFrames[j].FrameRectY)
                                            {
                                                frame.Frame.X = (e.Location.X + hScrollBar.Value);
                                                frame.Frame.Y = (int)ListOfFrames[j].FrameRectY;
                                                frame.Frame.Height = (e.Location.Y + vScrollBar.Value) - (int)ListOfFrames[j].FrameRectY;
                                                frame.Frame.Width = ((int)ListOfFrames[j].FrameRectX - frame.Frame.X);
                                                AniWidth.Value = frame.Frame.Width;
                                                AniHeight.Value = frame.Frame.Height;
                                            }
                                            if (e.X + hScrollBar.Value < ListOfFrames[j].FrameRectX && e.Y + vScrollBar.Value < ListOfFrames[j].FrameRectY)
                                            {
                                                frame.Frame.X = (e.Location.X + hScrollBar.Value);
                                                frame.Frame.Y = (e.Location.Y + vScrollBar.Value);
                                                frame.Frame.Height = ((int)ListOfFrames[j].FrameRectY - frame.Frame.Y);
                                                frame.Frame.Width = ((int)ListOfFrames[j].FrameRectX - frame.Frame.X);
                                                AniWidth.Value = frame.Frame.Width;
                                                AniHeight.Value = frame.Frame.Height;
                                            }
                                            if (e.X + hScrollBar.Value > ListOfFrames[j].FrameRectX && e.Y + vScrollBar.Value < ListOfFrames[j].FrameRectY)
                                            {
                                                frame.Frame.X = (int)ListOfFrames[j].FrameRectX;
                                                frame.Frame.Y = (e.Location.Y + vScrollBar.Value);
                                                frame.Frame.Height = ((int)ListOfFrames[j].FrameRectY - frame.Frame.Y);
                                                frame.Frame.Width = (e.Location.X + hScrollBar.Value) - (int)ListOfFrames[j].FrameRectX;
                                                AniWidth.Value = frame.Frame.Width;
                                                AniHeight.Value = frame.Frame.Height;
                                            }
                                            frame.FrameRect = true;
                                            ListOfFrames.Remove(ListOfFrames[j]);
                                            ListOfFrames.Insert(j, frame);
                                            //AniImage.Invalidate();
                                        }
                                    }
                                }
                                break;
                            case (int)types.attack:
                                {
                                    for (int j = 0; j < ListOfFrames.Count; j++)
                                    {
                                        if (AniFrames.SelectedItem.Equals(ListOfFrames[j].FrameName + ListOfFrames[j].FrameNumber))
                                        {
                                            Frames frame = ListOfFrames[j];
                                            if (e.X + hScrollBar.Value > ListOfFrames[j].AttackRectX && e.Y + vScrollBar.Value > ListOfFrames[j].AttackRectY)
                                            {
                                                frame.Attack.X = (int)ListOfFrames[j].AttackRectX;
                                                frame.Attack.Y = (int)ListOfFrames[j].AttackRectY;
                                                frame.Attack.Height = (e.Location.Y + vScrollBar.Value) - (int)ListOfFrames[j].AttackRectY;
                                                frame.Attack.Width = (e.Location.X + hScrollBar.Value) - (int)ListOfFrames[j].AttackRectX;
                                                AniWidth.Value = frame.Attack.Width;
                                                AniHeight.Value = frame.Attack.Height;
                                            }
                                            if (e.X + hScrollBar.Value < ListOfFrames[j].AttackRectX && e.Y + vScrollBar.Value > ListOfFrames[j].AttackRectY)
                                            {
                                                frame.Attack.X = (e.Location.X + hScrollBar.Value);
                                                frame.Attack.Y = (int)ListOfFrames[j].AttackRectY;
                                                frame.Attack.Height = (e.Location.Y + vScrollBar.Value) - (int)ListOfFrames[j].AttackRectY;
                                                frame.Attack.Width = ((int)ListOfFrames[j].AttackRectX - frame.Attack.X);
                                                AniWidth.Value = frame.Attack.Width;
                                                AniHeight.Value = frame.Attack.Height;
                                            }
                                            if (e.X + hScrollBar.Value < ListOfFrames[j].AttackRectX && e.Y + vScrollBar.Value < ListOfFrames[j].AttackRectY)
                                            {
                                                frame.Attack.X = (e.Location.X + hScrollBar.Value);
                                                frame.Attack.Y = (e.Location.Y + vScrollBar.Value);
                                                frame.Attack.Height = ((int)ListOfFrames[j].AttackRectY - frame.Attack.Y);
                                                frame.Attack.Width = ((int)ListOfFrames[j].AttackRectX - frame.Attack.X);
                                                AniWidth.Value = frame.Attack.Width;
                                                AniHeight.Value = frame.Attack.Height;
                                            }
                                            if (e.X + hScrollBar.Value > ListOfFrames[j].AttackRectX && e.Y + vScrollBar.Value < ListOfFrames[j].AttackRectY)
                                            {
                                                frame.Attack.X = (int)ListOfFrames[j].AttackRectX;
                                                frame.Attack.Y = (e.Location.Y + vScrollBar.Value);
                                                frame.Attack.Height = ((int)ListOfFrames[j].AttackRectY - frame.Attack.Y);
                                                frame.Attack.Width = (e.Location.X + hScrollBar.Value) - (int)ListOfFrames[j].AttackRectX;
                                                AniWidth.Value = frame.Attack.Width;
                                                AniHeight.Value = frame.Attack.Height;
                                            }
                                            frame.AttackRect = true;
                                            ListOfFrames.Remove(ListOfFrames[j]);
                                            ListOfFrames.Insert(j, frame);
                                            //AniImage.Invalidate();
                                        }
                                    }
                                }
                                break;
                        }
                    }
                }
            }
        }

        private void AniFrameRect_Click(object sender, EventArgs e)
        {
            if (SpriteSheet != null)
            {
                WhatToDraw = (int)types.frame;
                for (int j = 0; j < AniFrames.Items.Count; j++)
                {
                    if (AniFrames.GetSelected(j))
                    {
                        if (AniFrames.SelectedItem.Equals(ListOfFrames[j].FrameName + ListOfFrames[j].FrameNumber))
                        {
                            if (ListOfFrames[j].FrameRect)
                            {
                                AniRectX.Value = ListOfFrames[j].Frame.X;
                                AniRectY.Value = ListOfFrames[j].Frame.Y;
                                AniHeight.Value = ListOfFrames[j].Frame.Height;
                                AniWidth.Value = ListOfFrames[j].Frame.Width;
                            }
                            else
                            {
                                AniRectX.Value = 0;
                                AniRectY.Value = 0;
                                AniHeight.Value = 0;
                                AniWidth.Value = 0;
                            }
                        }
                    }
                }
            }
        }

        private void AniCollisionRect_Click(object sender, EventArgs e)
        {
            if (SpriteSheet != null)
            {
                WhatToDraw = (int)types.collision;
                for (int j = 0; j < AniFrames.Items.Count; j++)
                {
                    if (AniFrames.GetSelected(j))
                    {
                        if (AniFrames.SelectedItem.Equals(ListOfFrames[j].FrameName + ListOfFrames[j].FrameNumber))
                        {
                            if (ListOfFrames[j].CollisionRect)
                            {
                                AniRectX.Value = ListOfFrames[j].Collision.X;
                                AniRectY.Value = ListOfFrames[j].Collision.Y;
                                AniHeight.Value = ListOfFrames[j].Collision.Height;
                                AniWidth.Value = ListOfFrames[j].Collision.Width;
                            }
                            else
                            {
                                AniRectX.Value = 0;
                                AniRectY.Value = 0;
                                AniHeight.Value = 0;
                                AniWidth.Value = 0;
                            }
                        }
                    }
                }
            }
        }

        private void AniAttackRect_Click(object sender, EventArgs e)
        {
            if (SpriteSheet != null)
            {
                WhatToDraw = (int)types.attack;
                for (int j = 0; j < AniFrames.Items.Count; j++)
                {
                    if (AniFrames.GetSelected(j))
                    {
                        if (AniFrames.SelectedItem.Equals(ListOfFrames[j].FrameName + ListOfFrames[j].FrameNumber))
                        {
                            if (ListOfFrames[j].AttackRect)
                            {
                                AniRectX.Value = ListOfFrames[j].Attack.X;
                                AniRectY.Value = ListOfFrames[j].Attack.Y;
                                AniHeight.Value = ListOfFrames[j].Attack.Height;
                                AniWidth.Value = ListOfFrames[j].Attack.Width;
                            }
                            else
                            {
                                AniRectX.Value = 0;
                                AniRectY.Value = 0;
                                AniHeight.Value = 0;
                                AniWidth.Value = 0;
                            }
                        }
                    }
                }
            }
        }

        private void AniAnchorPoint_Click(object sender, EventArgs e)
        {
            if (SpriteSheet != null)
            {
                WhatToDraw = (int)types.anchor;
                for (int j = 0; j < ListOfFrames.Count; j++)
                {
                    if (AniFrames.GetSelected(j))
                    {
                        if (ListOfFrames[j].AnchorPoint)
                        {
                            AniPointX.Value = ListOfFrames[j].AnchorX;
                            AniPointY.Value = ListOfFrames[j].AnchorY;
                        }
                        else
                        {
                            AniRectX.Value = 0;
                            AniRectY.Value = 0;
                            AniHeight.Value = 0;
                            AniWidth.Value = 0;
                            AniPointX.Value = 0;
                            AniPointY.Value = 0;
                        }
                    }
                }
            }
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SpriteSheet = null;
            AniFrames.Items.Clear();
            AniRectX.Value = 0;
            AniRectY.Value = 0;
            AniPointX.Value = 0;
            AniPointY.Value = 0;
            AniWidth.Value = 0;
            AniHeight.Value = 0;
            AnimationName.Clear();
            AniImage.Invalidate();
            ListOfFrames.Clear();
            changeFrame = 0;
            TriggerName.Clear();
            FrameDuration.Enabled = false;
            AniRectX.Enabled = false;
            AniRectY.Enabled = false;
            AniWidth.Enabled = false;
            AniHeight.Enabled = false;
            AniPointX.Enabled = false;
            AniPointY.Enabled = false;
            AniFrameRect.Enabled = false;
            AniCollisionRect.Enabled = false;
            AniAttackRect.Enabled = false;
            AniAnchorPoint.Enabled = false;
            AniImage.Enabled = false;
            AniAdd.Enabled = false;
            AniRemove.Enabled = false;
            AniNexFrame.Enabled = false;
            AniPrevFrame.Enabled = false;
            AniReset.Enabled = false;
            AniPlay.Enabled = false;
            AniStop.Enabled = false;
            TriggerName.Enabled = false;
            PathSet = false;
            WhatToDraw = (int)types.nothing;
            i = 1;
            NumFrames = 0;
        }

        private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
        {
            AniImage.Invalidate();
        }

        private void hScrollBar_Scroll(object sender, ScrollEventArgs e)
        {
            AniImage.Invalidate();
        }

        private void saveBinaryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (PathSet)
            {
                if (AnimationName.Text.Length != 0)
                {
                    SaveFileDialog dlg = new SaveFileDialog();
                    dlg.Filter = "All Files(*.*)|*.*|Binary|*.bin";
                    dlg.DefaultExt = "bin";
                    dlg.FilterIndex = 2;
                    if (DialogResult.OK == dlg.ShowDialog())
                    {
                        BinaryWriter bin = new BinaryWriter(dlg.OpenFile(), Encoding.ASCII);
                        AnimationNameLength = AnimationName.Text.Length;
                        TriggerLength = TriggerName.Text.Length;
                        bin.Write(NumFrames);
                        for (int j = 0; j < ListOfFrames.Count; ++j)
                        {
                            bin.Write(ListOfFrames[j].FrameRect);
                            if (ListOfFrames[j].FrameRect)
                            {
                                bin.Write(ListOfFrames[j].Frame.X);
                                bin.Write(ListOfFrames[j].Frame.Y);
                                bin.Write(ListOfFrames[j].Frame.Height);
                                bin.Write(ListOfFrames[j].Frame.Width);
                            }

                            bin.Write(ListOfFrames[j].AttackRect);
                            if (ListOfFrames[j].AttackRect)
                            {
                                bin.Write(ListOfFrames[j].Attack.X);
                                bin.Write(ListOfFrames[j].Attack.Y);
                                bin.Write(ListOfFrames[j].Attack.Height);
                                bin.Write(ListOfFrames[j].Attack.Width);
                            }

                            bin.Write(ListOfFrames[j].CollisionRect);
                            if (ListOfFrames[j].CollisionRect)
                            {
                                bin.Write(ListOfFrames[j].Collision.X);
                                bin.Write(ListOfFrames[j].Collision.Y);
                                bin.Write(ListOfFrames[j].Collision.Height);
                                bin.Write(ListOfFrames[j].Collision.Width);
                            }

                            bin.Write(ListOfFrames[j].AnchorPoint);
                            if (ListOfFrames[j].AnchorPoint)
                            {
                                bin.Write(ListOfFrames[j].AnchorX);
                                bin.Write(ListOfFrames[j].AnchorY);
                            }
                            if (ListOfFrames[j].FrameRect && ListOfFrames[j].AnchorPoint)
                                bin.Write((int)(ListOfFrames[j].time*100.0f));
                        }
                        bin.Write(AnimationNameLength);
                        bin.Write(AnimationName.Text);
                        bin.Write(TriggerLength);
                        bin.Write(TriggerName.Text);
                        string temp = null;
                        for (int k = RelativePath.Length; k < FilePath.Length; k++)
                        {
                            temp += FilePath[k];
                        }
                        bin.Write(temp.Length);
                        bin.Write(temp);
                        bin.Close();
                    }
                }
                else
                {
                    MessageBox.Show("You do not have a animation name.", "I cant let you do that Star Fox!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                MessageBox.Show("Set a relative path through the import file option. Then import a bitmap or load in a file.", "Trust me, Im a doctor",
                                 MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
        }

        private void openBinaryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (PathSet)
            {
                OpenFileDialog dlg = new OpenFileDialog();
                dlg.Filter = "All Files(*.*)|*.*|Binary|*.bin";
                dlg.DefaultExt = "bin";
                dlg.FilterIndex = 2;
                if (DialogResult.OK == dlg.ShowDialog())
                {
                    SpriteSheet = null;
                    AniFrames.Items.Clear();
                    AniRectX.Value = 0;
                    AniRectY.Value = 0;
                    AniPointX.Value = 0;
                    AniPointY.Value = 0;
                    AniWidth.Value = 0;
                    AniHeight.Value = 0;
                    AnimationName.Clear();
                    AniImage.Invalidate();
                    ListOfFrames.Clear();
                    changeFrame = 0;
                    TriggerName.Clear();
                    WhatToDraw = (int)types.nothing;
                    i = 1;
                    NumFrames = 0;
                    BinaryReader bin = new BinaryReader(dlg.OpenFile(), Encoding.ASCII);
                    NumFrames = bin.ReadInt32();
                    for (int j = 0; j < NumFrames; ++j)
                    {
                        Frames frame = new Frames();
                        frame.FrameRect = bin.ReadBoolean();
                        if (frame.FrameRect)
                        {
                            frame.Frame.X = bin.ReadInt32();
                            frame.FrameRectX = frame.Frame.X;
                            frame.Frame.Y = bin.ReadInt32();
                            frame.FrameRectY = frame.Frame.Y;
                            frame.Frame.Height = bin.ReadInt32();
                            frame.FrameRectHeight = frame.Frame.Height;
                            frame.Frame.Width = bin.ReadInt32();
                            frame.FrameRectWidth = frame.Frame.Width;
                        }
                        frame.AttackRect = bin.ReadBoolean();
                        if (frame.AttackRect)
                        {
                            frame.Attack.X = bin.ReadInt32();
                            frame.AttackRectX = frame.Attack.X;
                            frame.Attack.Y = bin.ReadInt32();
                            frame.AttackRectY = frame.Attack.Y;
                            frame.Attack.Height = bin.ReadInt32();
                            frame.AttackRectHeight = frame.Attack.Height;
                            frame.Attack.Width = bin.ReadInt32();
                            frame.AttackRectWidth = frame.Attack.Width;
                        }
                        frame.CollisionRect = bin.ReadBoolean();
                        if (frame.CollisionRect)
                        {
                            frame.Collision.X = bin.ReadInt32();
                            frame.CollisionRectX = frame.Collision.X;
                            frame.Collision.Y = bin.ReadInt32();
                            frame.CollisionRectY = frame.Collision.Y;
                            frame.Collision.Height = bin.ReadInt32();
                            frame.CollisionRectHeight = frame.Collision.Height;
                            frame.Collision.Width = bin.ReadInt32();
                            frame.CollisionRectWidth = frame.Collision.Width;
                        }
                        frame.AnchorPoint = bin.ReadBoolean();
                        if (frame.AnchorPoint)
                        {
                            frame.AnchorX = bin.ReadInt32();
                            frame.AnchorY = bin.ReadInt32();
                        }
                        if (frame.AnchorPoint && frame.FrameRect)
                        {
                            frame.time = bin.ReadInt32();
                            frame.time /= 100.0f;
                        }

                        frame.FrameName = "frame ";
                        frame.FrameNumber = i;
                        AniFrames.Items.Add(frame.FrameName + frame.FrameNumber);
                        ListOfFrames.Add(frame);
                        AniFrames.SelectedIndex = (ListOfFrames.Count - 1);
                        i += 1;
                    }
                    AnimationNameLength = bin.ReadInt32();
                    AnimationName.Text = bin.ReadString();

                    TriggerLength = bin.ReadInt32();
                    TriggerName.Text = bin.ReadString();

                    string temp = null;
                    temp = RelativePath;
                    PathLength = bin.ReadInt32();
                    FilePath = bin.ReadString();
                    temp += FilePath;
                    FilePath = temp;
                    bin.Close();

                    SpriteSheet = new Bitmap(FilePath);
                    DRXSpriteSheet = texturesprite.LoadTexture(FilePath, 0);
                    vScrollBar.Maximum = SpriteSheet.Height;
                    vScrollBar.Minimum = 0;
                    hScrollBar.Maximum = SpriteSheet.Width;
                    hScrollBar.Minimum = 0;
                    AniFrames.Invalidate();
                    AniImage.Invalidate();
                    FrameDuration.Enabled = true;
                    AniRectX.Enabled = true;
                    AniRectY.Enabled = true;
                    AniWidth.Enabled = true;
                    AniHeight.Enabled = true;
                    AniPointX.Enabled = true;
                    AniPointY.Enabled = true;
                    AniFrameRect.Enabled = true;
                    AniCollisionRect.Enabled = true;
                    AniAttackRect.Enabled = true;
                    AniAnchorPoint.Enabled = true;
                    AniImage.Enabled = true;
                    AniAdd.Enabled = true;
                    AniRemove.Enabled = true;
                    AniNexFrame.Enabled = true;
                    AniPrevFrame.Enabled = true;
                    AniReset.Enabled = true;
                    AniPlay.Enabled = true;
                    AniStop.Enabled = true;
                    TriggerName.Enabled = true;
                }
            }
            else
            {
                MessageBox.Show("Set a relative path through the import file option. Then import a bitmap or load in a file.", "Trust me, Im a doctor",
                                 MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
        }

        private void saveXMLToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (PathSet)
            {
                if (AnimationName.Text.Length != 0)
                {
                    SaveFileDialog dlg = new SaveFileDialog();
                    dlg.Filter = "All Files(*.*)|*.*|Xml Files|*.xml";
                    dlg.DefaultExt = "xml";
                    dlg.FilterIndex = 2;

                    if (DialogResult.OK == dlg.ShowDialog())
                    {
                        List<Frames> list = new List<Frames>();
                        string aniname = AnimationName.Text; 
                        string triname = TriggerName.Text;
                        
                        string temp = null;
                        for (int k = RelativePath.Length; k < FilePath.Length; k++)
                        {
                            temp += FilePath[k];
                        }
                        Frames List = ListOfFrames[0];
                        List.AniName = aniname;
                        List.TriName = triname;
                        List.Bitmap = temp;
                        ListOfFrames.Remove(ListOfFrames[0]);
                        ListOfFrames.Insert(0, List);
                        foreach (Frames frame in ListOfFrames)
                        {
                            list.Add(frame);   
                        }
                        TextWriter writer = new StreamWriter(dlg.FileName);
                        XmlSerializer listserializer = new XmlSerializer(typeof(List<Frames>));
                        listserializer.Serialize(writer, list);
                        writer.Close();
                    }
                }
                else
                {
                    MessageBox.Show("You do not have a animation name.", "I cant let you do that Star Fox!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                MessageBox.Show("Set a relative path through the import file option. Then import a bitmap or load in a file.", "Trust me, Im a doctor",
                                 MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
        }
        private void openXMLToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (PathSet)
            {
                SpriteSheet = null;
                AniFrames.Items.Clear();
                AniRectX.Value = 0;
                AniRectY.Value = 0;
                AniPointX.Value = 0;
                AniPointY.Value = 0;
                AniWidth.Value = 0;
                AniHeight.Value = 0;
                AnimationName.Clear();
                AniImage.Invalidate();
                ListOfFrames.Clear();
                changeFrame = 0;
                TriggerName.Clear();
                WhatToDraw = (int)types.nothing;
                i = 1;

                OpenFileDialog dlg = new OpenFileDialog();
                dlg.Filter = "All Files(*.*)|*.*|Xml Files|*.xml";
                dlg.DefaultExt = "xml";
                dlg.FilterIndex = 2;
                if (DialogResult.OK == dlg.ShowDialog())
                {
                    List<Frames> list = null;

                    FileStream read = new FileStream(dlg.FileName, FileMode.Open);
                    XmlSerializer serial = new XmlSerializer(typeof(List<Frames>));

                    list = (List<Frames>)serial.Deserialize(read);
                    read.Close();
                    bool j = true;
                    foreach (Frames frame in list)
                    {
                        if (j)
                        {
                            AnimationName.Text = frame.AniName;
                            TriggerName.Text = frame.TriName;
                            string temp = null;
                            temp = RelativePath;
                            FilePath = frame.Bitmap;
                            temp += FilePath;
                            FilePath = temp;
                            j = false;
                            SpriteSheet = new Bitmap(FilePath);
                            DRXSpriteSheet = texturesprite.LoadTexture(FilePath, 0);
                        }
                        ListOfFrames.Add(frame);
                        AniFrames.Items.Add(frame.FrameName + frame.FrameNumber);

                        vScrollBar.Maximum = SpriteSheet.Height;
                        vScrollBar.Minimum = 0;
                        hScrollBar.Maximum = SpriteSheet.Width;
                        hScrollBar.Minimum = 0;
                        AniFrames.Invalidate();
                        AniImage.Invalidate();
                        FrameDuration.Enabled = true;
                        AniRectX.Enabled = true;
                        AniRectY.Enabled = true;
                        AniWidth.Enabled = true;
                        AniHeight.Enabled = true;
                        AniPointX.Enabled = true;
                        AniPointY.Enabled = true;
                        AniFrameRect.Enabled = true;
                        AniCollisionRect.Enabled = true;
                        AniAttackRect.Enabled = true;
                        AniAnchorPoint.Enabled = true;
                        AniImage.Enabled = true;
                        AniAdd.Enabled = true;
                        AniRemove.Enabled = true;
                        AniNexFrame.Enabled = true;
                        AniPrevFrame.Enabled = true;
                        AniReset.Enabled = true;
                        AniPlay.Enabled = true;
                        AniStop.Enabled = true;
                        TriggerName.Enabled = true;
                    }
                }
            }
            else
            {
                MessageBox.Show("Set a relative path through the import file option. Then import a bitmap or load in a file.", "Trust me, Im a doctor",
                                 MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
        }

        private void resourceDirectoryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dlg = new FolderBrowserDialog();
            dlg.SelectedPath = RelativePath;
            if (DialogResult.OK == dlg.ShowDialog())
            {
                RelativePath = dlg.SelectedPath;
            }
            PathSet = true;
        }
    }
}